import java.awt.BorderLayout;
import java.awt.Dimension;
import javax.swing.*;

import engine.Factory;
import flock.Flock;
import gui.ButtonPanel;
import gui.Canvas;
import gui.ResourceStrings;
import gui.SettingsPanel;

import java.awt.event.*;

/**
 * Creates an applet that be viewed over the web.
 * This class is the main GUI container for the animation engine.
 *
 * @author jianweigan
 */
@SuppressWarnings("serial")
public class Applet extends JApplet {

	public static final int DEFAULT_DELAY = 1000 / 25; // in milliseconds

	@SuppressWarnings("unchecked")
	/**
	 * Add new group classes to this array. e.g. Bouncers.class
	 * This way, new buttons will be added automatically.
	 */
	public static final Class[] SUPPORTED_GROUPS = new Class[] { Flock.class };

	private Canvas myDisplay;
	private Timer myTimer;

	/**
	 * Initializes the applet --- called by the browser.
	 */
	public void init() {
		init(new Dimension(Integer.parseInt(getParameter("width")), Integer
				.parseInt(getParameter("height"))));
	}

	/**
	 * Initializes the applet --- called by main.
	 * 
	 * Sets up the top level container for the animation engine GUI.
	 */
	public void init(Dimension size) {
		// create container to display animations
		myDisplay = new Canvas(size);
		myDisplay.setPreferredSize(size);
		myDisplay.setSize(size);
		myDisplay.setFocusable(true);
		myDisplay.requestFocus();

		SettingsPanel settings = new SettingsPanel(myDisplay);
		ButtonPanel commands = makeButtonPanel(settings);

		setJMenuBar(makeMenuBar());
		getContentPane().add(settings, BorderLayout.EAST);
		getContentPane().add(commands, BorderLayout.NORTH);
		getContentPane().add(myDisplay, BorderLayout.CENTER);
	}

	/**
	 * Creates a button panel, including factory buttons for all the
	 * types of groups in the supported groups list.
	 * @param settings settings panel of this applet
	 * @return correctly initialized button panel
	 */
	@SuppressWarnings("unchecked")
	private ButtonPanel makeButtonPanel(SettingsPanel settings) {
		ButtonPanel commands = new ButtonPanel(myDisplay, settings);
		for (Class groupType : SUPPORTED_GROUPS) {
			commands.addFactoryButton(new Factory(groupType));
		}
		return commands;
	}

	/**
	 * Starts the applet's action, i.e., starts the animation.
	 */
	public void start() {
		myTimer = new Timer(DEFAULT_DELAY, new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				myDisplay.update();
				myDisplay.repaint();
			}
		});
		myTimer.start();
	}

	/**
	 * Stops the applet's action, i.e., the animation.
	 */
	public void stop() {
		myTimer.stop();
	}

	/**
	 * Creates a menu bar.
	 * Currently has only a file menu.
	 * @return menu bar created
	 */
	private JMenuBar makeMenuBar() {
		JMenuBar menubar = new JMenuBar();
		menubar.add(makeFileMenu());
		return menubar;
	}

	/**
	 * Create a menu that will pop up when the menu button is pressed in the
	 * frame. Currently only contains 'Quit'
	 */
	private JMenu makeFileMenu() {
		JMenu result = new JMenu(ResourceStrings.getString("FileMenu"));
		result.add(new JSeparator());
		result
				.add(new AbstractAction(ResourceStrings
						.getString("QuitCommand")) {
					public void actionPerformed(ActionEvent e) {
						System.exit(0);
					}
				});
		return result;
	}
}